

import { Box3, LineSegments, LineBasicMaterial, Mesh, Object3D, Vector3, Group, BufferAttribute } from "three";

// 带标尺的包围盒
export default class RulerBox extends Group {
    constructor() {
        super();
    }

    _box = new Box3();
    _center = new Vector3();
    _size = new Vector3();
    //测量方法
    measure(object3d: Object3D) {
        // 计算包围盒
        this._box.setFromObject(object3d);
        // 计算包围盒的尺寸
        const size = this._box.getSize(this._size);
        // 计算包围盒的中心
        const center = this._box.getCenter(this._center);

        const direction = new Vector3(1, 0, 0);// 端线长度
        const verticalDirection = new Vector3(1, 0, 0);// 垂直方向

        // 四个方向的线
        // for (let angle of [0, 90, 180, 270]) {
        for (let angle of [0, 90]) {

            direction.set(1, 0, 0);
            direction.applyAxisAngle(new Vector3(0, 1, 0), Math.PI * (angle / 180));
            verticalDirection.set(1, 0, 0);
            verticalDirection.applyAxisAngle(new Vector3(0, 1, 0), (Math.PI) * ((angle + 90) / 180));

            const p = [];
            for (let endpoint of [
                verticalDirection,
                verticalDirection.clone().multiplyScalar(-1),
            ]) {

                const startP = direction.clone().multiply(size).multiplyScalar(0.5).add(endpoint.multiply(size).multiplyScalar(0.5));
                const endP = startP.clone().add(direction);
                p.push(...startP.toArray(), ...endP.toArray());
                p.unshift(...startP.add(direction.clone().multiplyScalar(0.5)).toArray());

            }

            const line = new LineSegments();
            let positionArray = new Float32Array(p);
            let position = new BufferAttribute(positionArray, 3, false);
            line.geometry.setAttribute("position", position);
            this.add(line);

        }

        // 计算主线段的端点坐标
        // const startPoint = new Vector3(center.x - size.x / 2, center.y + size.y / 2, center.z + size.z / 2);
        // const endPoint = new Vector3(center.x + size.x / 2, center.y + size.y / 2, center.z + size.z / 2);

        // // 计算左端短线段的第二个点
        // const startPointShort = new Vector3(startPoint.x, startPoint.y - shortLineLength, startPoint.z);
        // // 计算右端短线段的第二个点
        // const endPointShort = new Vector3(endPoint.x, endPoint.y - shortLineLength, endPoint.z);

        // const position = this.lin1Top1.geometry.getAttribute("position");
        // position.setXYZ(0, ...startPoint.toArray());
        // position.setXYZ(1, ...endPoint.toArray());
        // position.needsUpdate = true;

        // const lin4Side1 = new LineSegments();
        // let positionArray = new Float32Array(new Array(3 * 2).fill(0));
        // let position = new BufferAttribute(positionArray, 3, false);
        // geometry.setAttribute("position", position);



    }


}


